<template>
  <div>
    <!-- banner -->
    <div class="home-banner">
      <div class="banner-container">
        <!-- 联系方式 -->
        <h1 class="blog-title animated zoomIn">
          和其光 同其尘 挫其锐 解其纷
        </h1>
        <div class="blog-intro2" v-show="obj.output">
          {{ obj.output }} <span class="typed-cursor">|</span>
        </div>

        <!-- 联系方式 -->    <!--这里的联系方式用于移动端，浏览器端看不到  annotation by chen-->
        <div class="blog-contact">
          <a
            class="iconfont iconqq"
            target="_blank"
            href="http://wpa.qq.com/msgrd?v=3&uin=2776895893&site=qq&menu=yes"
          />
          <a
            target="_blank"
            href="https://github.com/chenchao1024"
            class="ml-5 mr-5 iconfont icongithub"
          />
          <a
            target="_blank"
            href="https://gitee.com/chen-1024"
            class="iconfont icongitee-fill-round"
          />
        </div>
      </div>
      <!-- 向下滚动 -->
      <div class="scroll-down" @click="scrollDown">
        <v-icon color="#fff" class="scroll-down-effects">
          mdi-chevron-down
        </v-icon>
      </div>
    </div>
    <!-- 主页文章 -->
    <v-row class="home-container">

      <!--主页的文章以及图片  annotation by chen-->
      <v-col md="9" cols="12">

        <v-card
          v-for="(item, index) of articleList"

          :key="item.id"
          class="animated zoomIn article-card"
          style="border-radius: 12px 8px 8px 12px"

        >
          <!-- 文章封面图 -->
          <div :class="isRight(index)">
            <a @click="detail(item)">
              <v-img
                class="on-hover"
                width="100%"
                height="100%"
                :src="item.img==null?
                                    'http://tamad.fun:9001/images/2022/08/24/1219a2e9af874169a902f19f24bb5c85.md.jpg':item.img"
              />
            </a>
          </div>
          <!-- 文章信息 -->
          <div class="article-wrapper">
            <div style="line-height:1.4">
              <a @click="detail(item)">
                {{ item.title }}
              </a>
            </div>
            <div class="article-info">
              <!-- 是否置顶 -->
              <span v-if="item.isTop === 1">
                <span style="color:#ff7242">
                  <i class="iconfont iconzhiding" /> 置顶
                </span>
                <span class="separator">|</span>
              </span>
              <v-avatar size="30" style="margin-bottom:0.5rem">
                <img :src="item.img" />
              </v-avatar>
              {{ item.nick }}
              <!-- 发表时间 -->
              <v-icon size="14">mdi-calendar-month-outline</v-icon>
              {{ item.time | date }}
              <span class="separator">|</span>
              <!-- 文章分类 -->
              <router-link :to="'/categories/' + item.categoryId">
                <v-icon size="14">mdi-inbox-full</v-icon>
                {{ item.category }}
              </router-link>
              <span class="separator">|</span>
              <v-icon size="14">mdi-tag-multiple</v-icon>
              {{ item.tag }}
              <!-- 文章标签 -->
              <router-link
                style="display:inline-block"
                :to="'/tags/' + tag.id"
                class="mr-1"
                v-for="tag of item.tagDTOList"
                :key="tag.id"
              >

              </router-link>
            </div>
            <!-- 文章内容 -->
            <div class="article-content">
              <span v-text="item.content"></span>
              <!--{{ item.content }}-->
            </div>
          </div>
        </v-card>

        <!-- 无限加载 -->
<!--        <infinite-loading @infinite="infiniteHandler">-->
<!--        <div slot="no-more" />-->
<!--        </infinite-loading>-->

        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size=5
            layout="total, sizes, prev, pager, next, jumper"
            :total=this.articleCount>
          </el-pagination>
        </div>
      </v-col>

      <!-- 博主信息 -->
      <v-col md="3" cols="12" class="d-md-block d-none">
        <div class="blog-wrapper">
          <!--博主信息（头像，博客数量统计，联系方式等） annotation by chen-->
          <v-card class="animated zoomIn blog-card mt-5">
            <!--博主信息 annotation by chen-->
            <div class="author-wrapper">
              <!-- 博主头像 annotation by chen-->
              <v-avatar size="110" style="margin-bottom:0.5rem">
                <img class="author-avatar" :src="form.avatar" />
              </v-avatar>
              <!--博主名字-->
              <div style="font-size: 1.375rem">{{ form.nick }}</div>
              <!--博主签名-->
              <div style="font-size: 0.875rem;">{{ form.introduce }}</div>
            </div>
            <!-- 博客信息 -->
            <div class="blog-info-wrapper">
              <div class="blog-info-data">
                <router-link to="/archives">
                  <div style="font-size: 0.875rem">文章</div>
                  <div style="font-size: 1.25rem">
                    {{ articleCount }}
                  </div>
                </router-link>
              </div>
              <div class="blog-info-data">
                <router-link to="/categories">
                  <div style="font-size: 0.875rem">分类</div>
                  <div style="font-size: 1.25rem">
                    {{ categoryCount }}
                  </div>
                </router-link>
              </div>
              <!--不要标签 changed by chen-->
              <div class="blog-info-data">
                <router-link to="/tags">
                  <div style="font-size: 0.875rem">标签</div>
                  <div style="font-size: 1.25rem">{{ tarCount }}</div>
                </router-link>
              </div>
            </div>
            <!-- 收藏按钮 -->
            <a class="collection-btn" @click="tip = true">
              <!--书签图案 annotation by chen-->
              <v-icon color="#fff" size="18" class="mr-1">mdi-bookmark</v-icon>
              加入书签
            </a>
            <div class="card-info-social">
              <a
                class="iconfont iconqq"
                target="_blank"
                href="http://wpa.qq.com/msgrd?v=3&uin=1608881588&site=qq&menu=yes"
              />
              <a
                target="_blank"
                href="https://github.com/maxuxu2000"
                class="ml-5 mr-5 iconfont icongithub"
              />
              <a
                target="_blank"
                href="https://gitee.com/maxuxu2000"
                class="iconfont icongitee-fill-round"
              />
            </div>
          </v-card>
          <!-- 网站信息 -->   <!--公告 annotation by chen-->
          <v-card class="blog-card animated zoomIn mt-5 big">
            <div class="web-info-title">
              <v-icon size="18">mdi-bell</v-icon>
              公告
              <!--: {{form.introduce}}https://www.lvjguo.top/img/bar2.90c9d9b3.gif-->
              <v-img src="../../assets/png/juansi.png" alt="hello" width="100%" height="100%" />
            </div>
            <div style="font-size:0.875rem">
              {{ blogInfo.notice }}
            </div>
          </v-card>

          <!--天气-->
          <br />
          <div>
            <div id="he-plugin-standard"></div>
          </div>

          <!-- 网站信息 -->   <!--网站资询 annotation by chen-->
          <v-card class="blog-card animated zoomIn mt-5">
            <div class="web-info-title">
              <v-icon size="18">mdi-chart-line</v-icon>
              网站资讯
            </div>
            <div class="web-info">
              <div style="padding:4px 0 0">
                运行时间:<span class="float-right">{{ time }}</span>
              </div>
              <div style="padding:4px 0 0">
                总访问量:<span class="float-right">{{ seeCount }}</span>
              </div>
            </div>
          </v-card>
        </div>
      </v-col>
    </v-row>
    <!-- 提示消息 -->
    <v-snackbar v-model="tip" top color="#49b1f5" :timeout="2000">
      按CTRL+D 键将本页加入书签
    </v-snackbar>
  </div>
</template>


<script>
import EasyTyper from "easy-typer-js";
import request from "../../utils/request";

let str = "";
export default {
  components: {},
  mounted() {
    // 获取文章个数
    request.get("/article/articleCount").then(res => {
      this.articleCount = res.data;
      // const totalPage = Math.ceil(this.articleCount / 5).toString();

    });
    request.post("/article/queryselfpagedesc", { limit: "5", page: "1" }).then(res => {
      this.articleList = res.data.records;
    });
    //文章分类个数
    request.get("/article/categoryList/all").then(res => {
      this.categoryCount = res.data.length;
    });

    //    标签
    request.get("/article/getTag/").then(res => {
      this.tarCount = res.data.length;
    });
    // 首页第一页内容（下面的几页就是分页）
    // request.get("/article/articleNew/"+this.form.username).then(res => {
    //   this.articleList = res.data;
    // });

  },

  created() {
    request.get("article/seeCount").then(res => {
      this.seeCount = res.data;
    });

    window.WIDGET = {
      "CONFIG": {
        "layout": "2",
        "width": 280,
        "height": 360,
        "background": "1",
        "dataColor": "FFFFFF",
        "borderRadius": "10",
        "key": "80d1312c7af044b09e79b4a54092fbdb" //这里换成自己的key！
      }
    };
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "https://widget.qweather.net/standard/static/js/he-standard-common.js?v=2.0";
    document.getElementsByTagName("head")[0].appendChild(script);

    this.init();

    this.timer = setInterval(this.runTime, 1000);

    if (this.$store.state.username === null) {
      this.$toast({ type: "warnning", message: "当前处于游客状态" });
    }

    if (this.form.avatar === "") {
      this.form.avatar = "http://tamad.fun:9001/images/2022/08/25/202208251629184.jpg";
    }

    if (this.form.introduce === "") {
      this.form.introduce = "路漫漫其修远兮，吾将上下而求索";
    }

    if (this.form.nick === "") {
      this.form.nick = "作者：Max-u-19 ";
    }
    // request.get("/article/selectArticleByFlag").then(res => {
    // request.get("/article/list/"+this.$store.state.username                                         ).then(res => {
    //     this.articleList = res.data.reverse()
    // });
  },

  data: function() {
    return {
      pageSize:"5",
      currentPage: "0",
      seeCount: 0,
      tip: false,
      time: "",
      obj: {
        output: "",
        isEnd: false,
        speed: 300,
        singleBack: false,
        sleep: 0,
        type: "rollback",
        backSpeed: 40,
        sentencePause: true
      },
      articleCount: '0',
      categoryCount: 0,
      tarCount: 0,
      articleList: [],
      blogInfo: {},
      current: 1,
      form: {
        introduce: "",
        avatar: "",
        nick: "",
        articleCount: "0",
        categoryCount: 0
      }

    };
  },

  methods: {
    handleSizeChange(val) {
      // const totalPage = Math.ceil(this.articleCount / val);
      // const target = (totalPage - this.currentPage + 1).toString();
      this.pageSize = val.toString();
      request.post("/article/queryselfpagedesc", {limit:this.pageSize,page:this.currentPage}).then(res => {
        this.articleList = res.data.records;
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val.toString();
      request.post("/article/queryselfpagedesc", {limit:this.pageSize,page:val.toString()}).then(res => {
        this.articleList = res.data.records;

      });
    },

    detail(ids) {

      if (ids.isEnable === false) {
        this.$toast({
          type: "warnning",
          message: "该文章已被封禁,请联系管理员"
        });
        return;
      }
      let id = ids.id;
      this.$router.push({
        path: "/articles/",
        query: { id: id }
      });
    },
    // 初始化
    init() {
      document.title = this.$route.meta.title;
      // 一言Api进行打字机循环输出效果
      fetch("https://v1.hitokoto.cn?c=i")
        .then(res => {
          return res.json();
        })
        .then(({ hitokoto }) => {
          this.initTyped(hitokoto);
        });
    },
    initTyped(input, fn, hooks) {
      const obj = this.obj;
      // eslint-disable-next-line no-unused-vars
      const typed = new EasyTyper(obj, input, fn, hooks);
    },
    scrollDown() {
      window.scrollTo({
        behavior: "smooth",
        top: document.documentElement.clientHeight
      });
    },
    runTime() {
      var timeold =
        new Date().getTime() - new Date("January 8,2022").getTime();
      var msPerDay = 24 * 60 * 60 * 1000;
      var daysold = Math.floor(timeold / msPerDay);
      var str = "";
      var day = new Date();
      str += daysold + "天";
      str += day.getHours() + "时";
      str += day.getMinutes() + "分";
      str += day.getSeconds() + "秒";
      this.time = str+'';
    },

    infiniteHandler($state) {
      let md = require("markdown-it")();
      this.axios
        // .get("/api/articles", {
        .get("/article/list", {
          params: {
            // current: this.current
          }
        })
        .then(({ data }) => {
          if (data.data.length) {
            // 去除markdown标签
            data.data.forEach(item => {
              item.articleContent = md
                .render(item.articleContent)
                .replace(/<\/?[^>]*>/g, "")
                .replace(/[|]*\n/, "")
                .replace(/&npsp;/gi, "");
            });
            this.articleList.push(...data.data);
            this.current++;
            $state.loaded();
          } else {
            $state.complete();
          }
        });
    }
  },
  computed: {
    isRight() {
      return function(index) {
        if (index % 2 == 0) {
          return "article-cover left-radius";
        }
        return "article-cover right-radius";
      };
    }
  }
};
</script>

<style lang="stylus">

.typed-cursor
  opacity: 1
  animation: blink 0.7s infinite

@keyframes blink
  0%
    opacity: 1
  50%
    opacity: 0
  100%
    opacity: 1
</style>

<style scoped>
.user-avatar {
  cursor: pointer;
  border-radius: 50%;
}

.home-banner {
  position: absolute;
  top: -60px;
  left: 0;
  right: 0;
  height: 100vh;
  background: url(../../assets/img/b3.jpg) center center / cover no-repeat;
  background-color: #49b1f5;
  /*background-attachment: fixed;*/
  text-align: center;
  color: #fff !important;
  animation: header-effect 1s;
}

.banner-container {
  margin-top: 43vh;
  line-height: 1.5;
  color: #eee;
}

.blog-contact a {
  color: #fff !important;
}

.card-info-social {
  line-height: 40px;
  text-align: center;
  font-size: 1.5rem;
  margin: 6px 0 -6px;
}

.left-radius {
  border-radius: 8px 0 0 8px !important;
  order: 0;
}

.right-radius {
  border-radius: 0 8px 8px 0 !important;
  order: 1;
}

.article-wrapper {
  font-size: 14px;
}

@media (min-width: 760px) {
  .blog-title {
    font-size: 2.5rem;
  }

  .blog-intro2 {
    font-size: 1.5rem;
  }

  .blog-contact {
    display: none;
  }

  .home-container {
    max-width: 1200px;
    margin: calc(100vh - 48px) auto 28px auto;
    padding: 0 5px;
  }

  .article-card {
    display: flex;
    align-items: center;
    height: 280px;
    width: 100%;
    margin-top: 20px;
  }

  .article-cover {
    overflow: hidden;
    height: 100%;
    width: 45%;
  }

  .on-hover {
    transition: all 0.6s;
  }

  .article-card:hover .on-hover {
    transform: scale(1.1);
  }

  .article-wrapper {
    padding: 0 2.5rem;
    width: 55%;
  }

  .article-wrapper a {
    font-size: 1.5rem;
    transition: all 0.3s;
  }
}

@media (max-width: 759px) {
  .blog-title {
    font-size: 26px;
  }

  .blog-contact {
    font-size: 1.25rem;
    line-height: 2;
  }

  .home-container {
    width: 100%;
    margin: calc(100vh - 66px) auto 0 auto;
  }

  .article-card {
    margin-top: 1rem;
  }

  .article-cover {
    border-radius: 8px 8px 0 0 !important;
    height: 230px !important;
    width: 100%;
  }

  .article-cover div {
    border-radius: 8px 8px 0 0 !important;
  }

  .article-wrapper {
    padding: 1.25rem 1.25rem 1.875rem;
  }

  .article-wrapper a {
    font-size: 1.25rem;
    transition: all 0.3s;
  }
}

.scroll-down {
  cursor: pointer;
  position: absolute;
  bottom: 0;
  width: 100%;
}

.scroll-down i {
  font-size: 2rem;
}

.article-wrapper a:hover {
  color: #8e8cd8;
}

.article-info {
  font-size: 95%;
  color: #858585;
  line-height: 2;
  margin: 0.375rem 0;
}

.article-info a {
  font-size: 95%;
  color: #858585 !important;
}

.article-content {
  line-height: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.blog-wrapper {
  position: sticky;
  top: 10px;
}

.blog-card {
  line-height: 2;
  padding: 1.25rem 1.5rem;
}

.author-wrapper {
  text-align: center;
}

.blog-info-wrapper {
  display: flex;
  justify-self: center;
  padding: 0.875rem 0;
}

.blog-info-data {
  flex: 1;
  text-align: center;
}

.blog-info-data a {
  text-decoration: none;
}

.collection-btn {
  text-align: center;
  z-index: 1;
  font-size: 14px;
  position: relative;
  display: block;
  background-color: #49b1f5;
  color: #fff !important;
  height: 32px;
  line-height: 32px;
  transition-duration: 1s;
  transition-property: color;
}

.collection-btn:before {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  background: #ff7242;
  content: "";
  transition-timing-function: ease-out;
  transition-duration: 0.5s;
  transition-property: transform;
  transform: scaleX(0);
  transform-origin: 0 50%;
}

.collection-btn:hover:before {
  transition-timing-function: cubic-bezier(0.45, 1.64, 0.47, 0.66);
  transform: scaleX(1);
}

.author-avatar {
  transition: all 0.5s;
}

.author-avatar:hover {
  transform: rotate(360deg);
}

.web-info {
  padding: 0.25rem;
  font-size: 0.875rem;
}

.scroll-down-effects {
  color: #eee !important;
  text-align: center;
  text-shadow: 0.1rem 0.1rem 0.2rem rgba(0, 0, 0, 0.15);
  line-height: 1.5;
  display: inline-block;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  animation: scroll-down-effect 1.5s infinite;
}

@keyframes scroll-down-effect {
  0% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
  50% {
    top: -16px;
    opacity: 1;
    filter: none;
  }
  100% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
}

.big i {
  color: #f00;
  animation: big 0.8s linear infinite;
}

@keyframes big {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
}
</style>
